﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RustDeskInitialize
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string configPath = Path.Combine(roamingPath, "rustdesk", "config", "RustDesk2.toml");
            if (!File.Exists(configPath))
            {
                MessageBox.Show("没有发现RustDesk配置目录，请确保已经正常安装了RustDesk", "系统提示");
            }
            txtConfigPath.Text = configPath;
            InitConfig();
        }
        private void InitConfig()
        {
            string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RustDeskInit.ini");
            IniUtil ini = new IniUtil(configFile, true);
            txtDomin.Text = ini.IniReadValue("config", "domain");
            txtIdPort.Text = ini.IniReadValue("config", "idport");
            txtZjPort.Text = ini.IniReadValue("config", "zjport");
            txtApiPort.Text = ini.IniReadValue("config", "apiport");
            txtKey.Text = ini.IniReadValue("config", "key");
        }
        private void SaveConfig()
        {
            string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RustDeskInit.ini");
            IniUtil ini = new IniUtil(configFile, true);
            ini.IniWriteValue("config", "domain", txtDomin.Text);
            ini.IniWriteValue("config", "idport", txtIdPort.Text);
            ini.IniWriteValue("config", "zjport", txtZjPort.Text);
            ini.IniWriteValue("config", "apiport", txtApiPort.Text);
            ini.IniWriteValue("config", "key", txtKey.Text);
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            if (!CheckIniFile())
                return;

            IniUtil ini = new IniUtil(txtConfigPath.Text);
            string domain = txtDomin.Text;
            string idPort = txtIdPort.Text;
            string zjPort = txtZjPort.Text;
            string apiPort = txtApiPort.Text;
            string key = txtKey.Text;
            idPort = string.IsNullOrEmpty(idPort) ? "" : ":" + idPort;
            zjPort = string.IsNullOrEmpty(zjPort) ? "" : ":" + zjPort;
            string apiUrl = string.IsNullOrEmpty(apiPort) ? "" :"http://"+ domain + ":" + apiPort;
            ini.IniWriteValue("init_operation", "rendezvous_server", $"'{domain}{idPort}'");
            ini.IniWriteValue("options", "relay-server", $"'{domain}{zjPort}'");
            ini.IniWriteValue("options", "api-server", $"'{apiUrl}'");
            ini.IniWriteValue("options", "custom-rendezvous-server", $"'{domain}{idPort}'");
            ini.IniWriteValue("options", "key", $"'{key}'");
            ini.CloseRustDeskToml();
            SaveConfig();
            MessageBox.Show("初始化操作完成。", "初始化");
        }

        private void btnGetCurrent_Click(object sender, EventArgs e)
        {
            StringBuilder config = new StringBuilder();
            if (!CheckIniFile())
                return;
            IniUtil ini = new IniUtil(txtConfigPath.Text);
            config.AppendLine("ID 服务器:" + ini.IniReadValue("options", "custom-rendezvous-server"));
            config.AppendLine("中继服务器:" + ini.IniReadValue("options", "relay-server"));
            config.AppendLine("API 服务器:" + ini.IniReadValue("options", "api-server"));
            config.AppendLine("Kye:" + ini.IniReadValue("options", "key"));
            ini.CloseRustDeskToml();
            MessageBox.Show(config.ToString(), "当前配置");
        }
        private bool CheckIniFile()
        {
            if (!File.Exists(txtConfigPath.Text))
            {
                MessageBox.Show("没有发现配置文件，路径：" + txtConfigPath.Text);
                return false;
            }
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
